<?php

namespace Php\Queue\work;

use Php\Queue\Query;
use Php\Queue\RedisDriver;

class Win extends QueueMaster
{
	public  $query;
	
	public function __construct(Query $query)
	{
		$this->query = $query;
	}
	
	public function run()
	{
		$redis = RedisDriver::connect($this->query->config, 0);
		while (1) {
			// 将达到指定时间的消息加入到队列.
			$this->query->migrateExpiredJobs($redis, 100);
			$result = $this->query->pop($redis);
			if ($result) {
				$class = $result['job'];
				unset($result['job']);
				$class = new $class;
				$class->job($this->query, $result);
			} else {
				sleep(2);
				// 当队列中没有消息时,将没有标记成功的消息恢复到队列中
				$this->query->recoverMessage($redis);
			}
		}
	}
}